﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;
using Castle.Windsor;

public partial class School_ClassGrid : ContainerBasePage
{
    private static readonly ITeachClsCauseLogic _teaClsCauLogic = Container<ITeachClsCauseLogic>.Instance;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind();
    }


    //绑定Repeater列表
    private void Bind()
    {
        string strGradeID = HFGradeIDValue.Value;
        Hashtable ht = new Hashtable();
        ht.Add("Grade", strGradeID);
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        ht.Add("IsEffect", 1);

        IClsInfoLogic clsLogic = Container<IClsInfoLogic>.Instance;
        List<VClsInfo> list = (List<VClsInfo>)clsLogic.FindClassInfo(ht);

        Repeater1.DataSource = list;
        Repeater1.DataBind();
    }

    //获取班级列表
    //[System.Web.Services.WebMethod]
    //public static int BindDataByGradeID(int GradeID)
    //{
    //    Hashtable ht = new Hashtable();
    //    ht.Add("Grade", GradeID);
    //    ht.Add("SchoolMark", strSchoolMarck);
    //    ht.Add("IsEffect", 1);

    //    IClsInfoLogic clsLogic = Container<IClsInfoLogic>.Instance;
    //    List<VClsInfo> list = (List<VClsInfo>)clsLogic.FindClassInfo(ht);

    //    viewData = list;

    //    return 0;
    //}

    //根据班级ID获取班级信息
    [System.Web.Services.WebMethod]
    public static ClsInfo GetClassInfoByClsID(int clsID)
    {
        IClsInfoLogic classLogic = Container<IClsInfoLogic>.Instance;
        ClsInfo classInfo =  classLogic.FindById(clsID);
        return classInfo;
    }

    //获取班级ID为clsID的学生有效人数
    [System.Web.Services.WebMethod]
    public static int GetStudentCountByClsID(int clsID)
    {
        Hashtable ht = new Hashtable();
        ht.Add("ClsID", clsID);
        ht.Add("IsEffect", 1);

        int iCount = Container<IStudentInfoLogic>.Instance.FindCount(ht);
        return iCount;
    }

    //根据班级ID删除班级
    [System.Web.Services.WebMethod]
    public static int DeleteClassInfo(int clsID)
    {
        //判断班级下面是否存在有效学生，如果存在，则返回-1
        Hashtable ht = new Hashtable();
        ht.Add("ClsID", clsID);
        ht.Add("IsEffect", 1);

        int iCount = Container<IStudentInfoLogic>.Instance.FindCount(ht);

        if (iCount > 0)
            return -1;
        
        //如果不存在，则删除
        int iResult = Container<IClsInfoLogic>.Instance.DeleteByID(clsID);
        Container<IClassHistoryLogic>.Instance.DeleteByClassID(clsID, SchoolMark);

        return iResult;

    }

    [System.Web.Services.WebMethod]
    public static int ValidateUsed(ClsInfo classInfo)
    {
        int result = 0;
        IClsInfoLogic clsLogic = Container<IClsInfoLogic>.Instance;

        //1.新增
        if (classInfo.RID == 0)
        {
            //Hashtable tableNo = new Hashtable();
            //tableNo.Add("ClsNo", classInfo.ClsNo);
            //tableNo.Add("SchoolMark", strSchoolMarck);
            //tableNo.Add("IsEffect", 1);
            

            //result = clsLogic.FindCount(tableNo);
            ////班级编号重复
            //if (result > 0)
            //    return 1;

           /* Hashtable tableName = new Hashtable();
            tableName.Add("ClsName", classInfo.ClsName);
            tableName.Add("SchoolMark", strSchoolMarck);
            tableName.Add("IsEffect", 1);


            result = clsLogic.FindCount(tableName);

            //班级名称重复
            if (result > 0)
                return 2;*/



        }
        //2.修改
        else
        {
            ClsInfo cls = clsLogic.FindById(classInfo.RID);

            if (cls.ClsNo != classInfo.ClsNo)
            {
                Hashtable tableNo = new Hashtable();
                tableNo.Add("ClsNo", classInfo.ClsNo);
                tableNo.Add("SchoolMark", Common.GetConstSchoolMark());
                tableNo.Add("IsEffect", 1);
                tableNo.Add("Grade", classInfo.Grade);

                result = clsLogic.FindCount(tableNo);
                //班级编号重复
                if (result > 0)
                    return 1;
            }

            /*if(cls.ClsName != classInfo.ClsName)
            {
                Hashtable tableName = new Hashtable();
                tableName.Add("ClsName", classInfo.ClsName);
                tableName.Add("SchoolMark", strSchoolMarck);
                tableName.Add("IsEffect", 1);


                result = clsLogic.FindCount(tableName);
                //班级名称重复
                if (result > 0)
                    return 2;
            }*/
        }

        return result;
    }

    //添加新班级
    [System.Web.Services.WebMethod]
    public static int AddClassInfo(ClsInfo classInfo)
    {
        classInfo.SchoolMark = Common.GetConstSchoolMark();
        int iResult = Container<IClsInfoLogic>.Instance.AddNewClass(classInfo);
        if (classInfo.HeaderID != -1)
        {
            SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", si.SchoolMark);
            table.Add("SchoolYear", si.SchoolYear);
            table.Add("Term", si.SchoolTerm);
            table.Add("RoleID", RoleKeyEnum.Class_Header);
            IList<TeacherRoles> tr = Container<ITeacherRolesLogic>.Instance.FindAll(table);
            if (!CheckHeader(tr, classInfo.HeaderID))
            {
                TeacherRoles t = new TeacherRoles();
                t.RoleID = (int)RoleKeyEnum.Class_Header;
                t.SchoolMark = si.SchoolMark;
                t.SchoolYear = si.SchoolYear;
                t.TeacherID = classInfo.HeaderID;
                t.Term = si.SchoolTerm;
                Container<ITeacherRolesLogic>.Instance.Create(t);
            }
        }
        return iResult;
    }
    private static bool CheckHeader(IList<TeacherRoles> tr,int headerid)
    {
        foreach (TeacherRoles t in tr)
        {
            if (t.TeacherID == headerid)
            {
                return true;
            }
        }
        return false;
    }
    //更新班级信息
    [System.Web.Services.WebMethod]
    public static int UpdateClassInfo(ClsInfo classInfo)
    {
        classInfo.SchoolMark = Common.GetConstSchoolMark();
        classInfo.IsEffect = true;
        //更新班级信息
        int iResult = Container<IClsInfoLogic>.Instance.UpdateClsExceptClsNO(classInfo);
        if (classInfo.HeaderID != -1)
        {
            SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", si.SchoolMark);
            table.Add("SchoolYear", si.SchoolYear);
            table.Add("Term", si.SchoolTerm);
            table.Add("RoleID", RoleKeyEnum.Class_Header);
            IList<TeacherRoles> tr = Container<ITeacherRolesLogic>.Instance.FindAll(table);

            if (!CheckHeader(tr, classInfo.HeaderID))
            {
                TeacherRoles t = new TeacherRoles();
                t.RoleID = (int)RoleKeyEnum.Class_Header;
                t.SchoolMark = si.SchoolMark;
                t.SchoolYear = si.SchoolYear;
                t.TeacherID = classInfo.HeaderID;
                t.Term = si.SchoolTerm;
                Container<ITeacherRolesLogic>.Instance.Create(t);
            }

            table = new Hashtable();
            table.Add("SchoolMark", si.SchoolMark);
            table.Add("IsEffect", 1);
            IList<ClsInfo> classes = Container<IClsInfoLogic>.Instance.FindAll(table);
            foreach (TeacherRoles t in tr)
            {
                if (!CheckHeader2(t.TeacherID, classes))
                {
                    table = new Hashtable();
                    table.Add("RID", t.RID);
                    Container<ITeacherRolesLogic>.Instance.DeleteBy(table);
                }
            }
        }
        return iResult;
    }
    private static bool CheckHeader2(int id, IList<ClsInfo> classes)
    {
        foreach (ClsInfo c in classes)
        { 
            if(c.HeaderID == id)
            {
                return true;
            }
        }
        return false;
    }
    //更新班号
    [System.Web.Services.WebMethod]
    public static int UpdateClassNo(IList<ClsInfo> listClsInfo)
    {
        //对象有效字段：RID, ClsNo, Grade
        //在修改班级编号的同时需要修改班级名称
        int iResult = Container<IClsInfoLogic>.Instance.UpdateClsNO((List<ClsInfo>)listClsInfo);


        return iResult;
    }

    //获取所有有效的教师信息
    [System.Web.Services.WebMethod]
    public static List<TeacherInfo> GetAllTeacher(int grade)
    {
        Hashtable ht = new Hashtable();
        List<TeacherInfo> temp = new List<TeacherInfo>();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        ht.Add("IsEffect", 1);
        ht.Add("IsAdmin", 0);
        List<TeacherInfo> teachers = (List<TeacherInfo>)Container<ITeacherInfoLogic>.Instance.FindAll(ht);
        
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        ht = new Hashtable();
        ht.Add("SchoolMark", si.SchoolMark);
        ht.Add("Grade", grade);
        ht.Add("SchoolYear", si.SchoolYear);
        //ht.Add("Term", si.SchoolTerm);
        IList<GradeGroup> gg = Container<IGradeGroupLogic>.Instance.FindAll(ht);

        foreach (TeacherInfo t in teachers)
        {
            if (CheckTeacher(t.RID, gg))
            {
                temp.Add(t);
            }
        }
        return temp;
    }

    private static bool CheckTeacher(int id, IList<GradeGroup> gg)
    {
        foreach (GradeGroup g in gg)
        {
            if (g.TeacherID == id)
            {
                return true;
            }
        }
        return false;
    }
    //获取班级课程和教师信息
    [System.Web.Services.WebMethod]
    public static List<CustomTeachClsCause> GetTeacherCauseByClassId(int classID)
    {
        Hashtable ht = new Hashtable();
        ht.Add("ClsRID", classID);
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        ITeachClsCauseLogic clsCauseTeacherLogic = Container<ITeachClsCauseLogic>.Instance;
        List<CustomTeachClsCause> clsCauseTeacherInfoList = new List<CustomTeachClsCause>();
        clsCauseTeacherInfoList = (List<CustomTeachClsCause>)clsCauseTeacherLogic.GetClassCauseTeacherList(ht);

        return clsCauseTeacherInfoList;
    }
}
